﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Minesweeper.Models;
using Minesweeper.Windows.Controls;
using Minesweeper.Converter;

namespace Minesweeper
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {


        protected Options _options = new Options() { 
            ButtonCountX = 30, 
            ButtonCountY = 16, 
            ButtonHeight = 25, 
            ButtonWidth  = 25,
            BombCount    = 99
        };

        protected Board _board;

        protected FieldButton[,] _fieldButtons;

        protected int _openButtons = 0;

        public MainWindow()
        {
            InitializeComponent();
            window.DataContext = _options;
            _InitializeButtons();
            _board = new Board(_options.ButtonCountX, _options.ButtonCountY, _options.BombCount);
            //_InitializeDebugView();
            MainGrid.SizeChanged += new SizeChangedEventHandler(MainGrid_SizeChanged);
            
        }

        void MainGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Grid grid = sender as Grid;

            Double width = grid.ActualWidth;
            Double height = grid.ActualHeight;

            _options.ButtonWidth  = width / _options.ButtonCountX;
            _options.ButtonHeight = height / _options.ButtonCountY;

        }

        /// <summary>
        /// Initialisiert die Buttons auf der Spielfläche
        /// </summary>
        protected void _InitializeButtons()
        {
            _fieldButtons = new FieldButton[_options.ButtonCountX, _options.ButtonCountY];
            for (int x = 0; x < _options.ButtonCountX; x++)
            {
                for (int y = 0; y < _options.ButtonCountY; y++)
                {
                    // Button erstellen
                    FieldButton button = new FieldButton() { X = x, Y = y };

                    // Bind für das Margin hinzufügen
                    MultiBinding bndMargin = new MultiBinding() { Converter = new MultiMarginConverter(), ConverterParameter = button };
                    bndMargin.Bindings.Add(new Binding("ButtonWidth") { Mode = BindingMode.OneWay });
                    bndMargin.Bindings.Add(new Binding("ButtonHeight") { Mode = BindingMode.OneWay });
                    button.SetBinding(FieldButton.MarginProperty, bndMargin);

                    // EventHandler für Click-Event hinzufügen
                    button.Click += new RoutedEventHandler(button_Click);

                    // Style hinzufügen
                    button.SetResourceReference(Control.StyleProperty, "FieldButton");

                    // Button zum Grid hinzufügen
                    MainGrid.Children.Add(button);

                    // Internes Array von Buttons
                    _fieldButtons[x, y] = button;

                    // Anzahl offener Buttons inkrementieren
                    _openButtons++;
                }

            }

        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            FieldButton button = sender as FieldButton;

            _openButton(button.X, button.Y);

            if (_board.Fields[button.X, button.Y].FieldType == Field.Types.Bomb)
            {
                lostGame();
            }
            else if (_board.Fields[button.X, button.Y].FieldType == Field.Types.Empty)
            {
                List<Coordinate> affectedCoordinates = new List<Coordinate>();
                _board.getAffectedFieldsArround(affectedCoordinates, button.X, button.Y);

                foreach (Coordinate coord in affectedCoordinates)
                {
                    _openButton(coord.X, coord.Y);
                }
            }
        }

        protected void _openButton(int x, int y)
        {
            if (_fieldButtons[x, y].IsEnabled == true)
            {
                String fieldType = String.Empty;
                if ((int)_board.Fields[x, y].FieldType == 9)
                {
                    fieldType = "BB";
                }
                else if ((int)_board.Fields[x, y].FieldType != 0)
                {
                    int fieldTypeInt = (int)_board.Fields[x, y].FieldType;
                    fieldType = fieldTypeInt.ToString();
                }
                _fieldButtons[x, y].Content = fieldType;
                _fieldButtons[x, y].IsEnabled = false;

                _openButtons--;

                if (_openButtons <= _options.BombCount)
                {
                    wonGame();
                }
            }

        }

        protected void lostGame()
        {
            for (int x = 0; x < _options.ButtonCountX; x++)
            {
                for (int y = 0; y < _options.ButtonCountY; y++)
                {
                    FieldButton current = _fieldButtons[x, y];
                    if (current.IsEnabled == true)
                    {
                        current.IsEnabled = false;
                        if (_board.Fields[x, y].FieldType == Field.Types.Bomb)
                        {
                            current.Content = "BB";
                        }
                    }
                }
            }
            MessageBox.Show("Verloooooooorn!");
        }

        protected void wonGame()
        {
            for (int x = 0; x < _options.ButtonCountX; x++)
            {
                for (int y = 0; y < _options.ButtonCountY; y++)
                {
                    FieldButton current = _fieldButtons[x, y];
                    if (current.IsEnabled == true)
                    {
                        current.IsEnabled = false;
                        if (_board.Fields[x, y].FieldType == Field.Types.Bomb)
                        {
                            current.Content = "BB";
                        }
                    }
                }
            }
            MessageBox.Show("Gewonnen!");
        }
    }
}
